/*
 * Copyright 2013 Alibaba.com All right reserved. This software is the
 * confidential and proprietary information of Alibaba.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Alibaba.com.
 */
package com.yunos.tv.common.util;


import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import org.apache.commons.lang.StringUtils;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

/**
 * 类Dom4jDemo.java的实现描述：TODO 类实现描述
 * @author riqi 2013-7-3 上午11:16:45
 */
public class Dom4jDemo {

    private static final String filenameFrom = "D:/workspace/Java/file/demoFrom.xml";

    private static final String filenameTo   = "D:/workspace/Java/file/demoTo.xml";

    private static SAXReader    saxReader    = new SAXReader();

    public static void main(String[] args) throws FileNotFoundException {

        ArrayList<HashMap<String, String>> personList = new ArrayList<HashMap<String, String>>();
        try {
            // 读取整个xml文档
            Document doc = saxReader.read(new File(filenameFrom));
            // Document doc = saxReader.read(new BufferedInputStream(new FileInputStream(new File(filenameFrom))));

            // 遍历文档节点
            for (Iterator iterator = doc.nodeIterator(); iterator.hasNext();) {
                Node node = (Node) iterator.next();

                node.getName();// 节点名称
                node.getNodeTypeName();//节点类型名称
            }

            Element rootElement = doc.getRootElement();// 获取根节点
            for (Iterator<?> iterator = rootElement.elementIterator(); iterator.hasNext();) {
                Element element = (Element) iterator.next();

                HashMap<String, String> personMap = new HashMap<String, String>();

                Attribute genderAttr = element.attribute("gender");
                personMap.put("gender", StringUtils.trim(genderAttr.getValue()));

                for (Iterator<?> iterator2 = element.elementIterator(); iterator2.hasNext();) {
                    Element element2 = (Element) iterator2.next();

                    personMap.put(element2.getName(), element2.getTextTrim());
                }

                personList.add(personMap);
            }

        } catch (DocumentException e) {
            e.printStackTrace();
        }
        System.out.println(personList);

        String xmlStr = "<Cmd><Type>aaaa</Type><Cont>bbbb</Cont></Cmd>";
        try {
            Document document = DocumentHelper.parseText(xmlStr);// 将String转换成文档对象
            System.out.println(document.asXML());
        } catch (DocumentException e) {
            e.printStackTrace();
        }

    }
}
